Fuzz 工具 WinAFL 的使用感受—对 FreeImage 图片解析库进行模糊测试
DynamoRIO-7.0.0\bin32> drrun,exe -t drcov -- [程序/模块名] [参数]
.logopen E:\log.txt
lmm FreeImage
x FreeImage*
bm FreeImge!FreeImage* ".echo; kb 1; g"
/*
test.exe
*/
using namespace std;
extern "C" __declspec(dllexport) int main(int argc, char** argv);
void FreeImage_test(HINSTANCE hinstLib, wchar_t* pathfile);
wchar_t* charToWChar(const char* text);
// 加载卸载函数
typedef DWORD(__stdcall *FreeImage_GetFileTypeU)(const wchar_t* lpszPathName, int flag);
typedef DWORD(__stdcall *FreeImage_Initialise)(BOOL load_local_plugins_only);
typedef DWORD(__stdcall *FreeImage_DeInitialise)();
typedef DWORD(__stdcall *FreeImage_LoadU)(DWORD format, const wchar_t* lpszPathName, int flag);
typedef DWORD(__stdcall *FreeImage_UnLoad)(DWORD dib);
// 获取信息函数
typedef DWORD(__stdcall *FreeImage_GetFIFFromFilenameU)(const wchar_t* lpszPathName); FreeImage_GetFIFFromFilenameU GetFIFFromFilenameU;
typedef DWORD(__stdcall *FreeImage_GetDotsPerMeterX)(DWORD dib); FreeImage_GetDotsPerMeterX GetDotsPerMeterX;
typedef DWORD(__stdcall *FreeImage_GetDotsPerMeterY)(DWORD dib); FreeImage_GetDotsPerMeterY GetDotsPerMeterY;
typedef DWORD(__stdcall *FreeImage_GetTransparencyCount)(DWORD dib); FreeImage_GetTransparencyCount GetTransparencyCount;
typedef DWORD(__stdcall *FreeImage_GetTransparencyTable)(DWORD dib); FreeImage_GetTransparencyTable GetTransparencyTable;
typedef DWORD(__stdcall *FreeImage_GetBlueMask)(DWORD dib); FreeImage_GetBlueMask GetBlueMask;
typedef DWORD(__stdcall *FreeImage_GetRedMask)(DWORD dib); FreeImage_GetRedMask GetRedMask;
typedef DWORD(__stdcall *FreeImage_GetThumbnail)(DWORD dib); FreeImage_GetThumbnail GetThumbnail;
typedef DWORD(__stdcall *FreeImage_GetPalette)(DWORD dib); FreeImage_GetPalette GetPalette;
typedef DWORD(__stdcall *FreeImage_GetGreenMask)(DWORD dib); FreeImage_GetGreenMask GetGreenMask;
typedef DWORD(__stdcall *FreeImage_GetImageType)(DWORD dib); FreeImage_GetImageType GetImageType;
typedef DWORD(__stdcall *FreeImage_GetICCProfile)(DWORD dib); FreeImage_GetICCProfile GetICCProfile;
typedef DWORD(__stdcall *FreeImage_GetBackgroundColor)(DWORD dib); FreeImage_GetBackgroundColor GetBackgroundColor;
typedef DWORD(__stdcall *FreeImage_GetInfo)(DWORD dib); FreeImage_GetInfo GetInfo; // 不确定的
typedef DWORD(__stdcall *FreeImage_GetFormatFromFIF)(DWORD dib); FreeImage_GetFormatFromFIF GetFormatFromFIF;
typedef DWORD(__stdcall *FreeImage_GetFIFExtensionList)(DWORD dib); FreeImage_GetFIFExtensionList GetFIFExtensionList;
typedef DWORD(__stdcall *FreeImage_GetMetadataCount)(DWORD format, DWORD dib); FreeImage_GetMetadataCount GetMetadataCount;
FreeImage_Initialise Initialise;
FreeImage_GetFileTypeU LoadFileType;
FreeImage_LoadU LoadU; DWORD load;
FreeImage_UnLoad UnLoad;
FreeImage_DeInitialise DeInitialise;
int main(int argc, char** argv)
{
if (argc < 2) {
printf("Usage: %s <xml file>\n", argv[0]);
return 0;
}
wchar_t* PathName = charToWChar(argv[1]);
HINSTANCE hinstLib; BOOL fFreeResult, fRunTimeLinkSuccess = FALSE; DWORD Error = NULL;
hinstLib = LoadLibrary(TEXT("E:\\FreeImage.dll"));
if (hinstLib != NULL)
{
fRunTimeLinkSuccess = TRUE;
Initialise = (FreeImage_Initialise)GetProcAddress(hinstLib, (LPCSTR)163); // 初始化 FreeImage 库
LoadFileType = (FreeImage_GetFileTypeU)GetProcAddress(hinstLib, (LPCSTR)126);// 获取位图文件类型
LoadU = (FreeImage_LoadU)GetProcAddress(hinstLib, (LPCSTR)181); // 加载位图
UnLoad = (FreeImage_UnLoad)GetProcAddress(hinstLib, (LPCSTR)242);// 卸载位图
DeInitialise = (FreeImage_DeInitialise)GetProcAddress(hinstLib, (LPCSTR)83);//卸载 FreeImage 库
// 获取信息函数
LoadFileType = (FreeImage_GetFileTypeU)GetProcAddress(hinstLib, (LPCSTR)126);
GetFIFFromFilenameU = (FreeImage_GetFIFFromFilenameU)GetProcAddress(hinstLib, (LPCSTR)118);
GetDotsPerMeterX = (FreeImage_GetDotsPerMeterX)GetProcAddress(hinstLib, (LPCSTR)112);
GetDotsPerMeterY = (FreeImage_GetDotsPerMeterY)GetProcAddress(hinstLib, (LPCSTR)113);
GetTransparencyCount = (FreeImage_GetTransparencyCount)GetProcAddress(hinstLib, (LPCSTR)155);
GetTransparencyTable = (FreeImage_GetTransparencyTable)GetProcAddress(hinstLib, (LPCSTR)156);
GetBlueMask = (FreeImage_GetBlueMask)GetProcAddress(hinstLib, (LPCSTR)105);
GetRedMask = (FreeImage_GetRedMask)GetProcAddress(hinstLib, (LPCSTR)145);
GetGreenMask = (FreeImage_GetGreenMask)GetProcAddress(hinstLib, (LPCSTR)128);
GetThumbnail = (FreeImage_GetThumbnail)GetProcAddress(hinstLib, (LPCSTR)154);
GetPalette = (FreeImage_GetPalette)GetProcAddress(hinstLib, (LPCSTR)141);
GetImageType = (FreeImage_GetImageType)GetProcAddress(hinstLib, (LPCSTR)132);
GetICCProfile = (FreeImage_GetICCProfile)GetProcAddress(hinstLib, (LPCSTR)131);
GetBackgroundColor = (FreeImage_GetBackgroundColor)GetProcAddress(hinstLib, (LPCSTR)103);
GetInfo = (FreeImage_GetInfo)GetProcAddress(hinstLib, (LPCSTR)133);
GetFormatFromFIF = (FreeImage_GetFormatFromFIF)GetProcAddress(hinstLib, (LPCSTR)127);
GetFIFExtensionList = (FreeImage_GetFIFExtensionList)GetProcAddress(hinstLib, (LPCSTR)116);
GetMetadataCount = (FreeImage_GetMetadataCount)GetProcAddress(hinstLib, (LPCSTR)139);
(Initialise)(FALSE);
FreeImage_test(hinstLib, PathName);
(DeInitialise)();
fFreeResult = FreeLibrary(hinstLib);
}
if (!fRunTimeLinkSuccess)
cout << "加载函数失败, Error: " << Error << endl;
return 0;
}
void FreeImage_test(HINSTANCE hinstLib, wchar_t* pathfile)
{
// 加载函数
DWORD FileType = (LoadFileType)(pathfile, 0);
load = (LoadU)(FileType, pathfile, 0);
// 获取信息函数
GetFIFFromFilenameU(pathfile);
GetDotsPerMeterX(load);
GetDotsPerMeterY(load);
GetTransparencyCount(load);
GetTransparencyTable(load);
GetBlueMask(load);
GetRedMask(load);
GetGreenMask(load);
GetThumbnail(load);
GetPalette(load);
GetImageType(load);
GetICCProfile(load);
GetInfo(load);
GetFormatFromFIF(load);
GetFIFExtensionList(load);
// 卸载函数
(UnLoad)(load);
}
wchar_t* charToWChar(const char* text)
{
size_t size = strlen(text) + 1;
wchar_t* wa = new wchar_t[size];
mbstowcs(wa, text, size);
return wa;
}
winafl\python winafl-cmin.py --working-dir E:\winafl\bin32
-D E:\DynamoRIO-7.0.0\bin32
-t 100000
-i [案例路径]
-o [输出案例路径]
-coverage_module FreeImage.dll
-target_module test.exe
-target_method main
-nargs 2 -- E:\test.exe @@ // 表示引用
winafl\bin32\afl-fuzz.exe -i [输入案例路径]
-o [结果输出路径]
-D E:\DynamoRIO-7.0.0\bi3n2
-t 20000
-- -coverage_module FreeImage.dll
-fuzz_iterations 5000
-target_module test.exe
-target_offset [函数偏移] // 这里进行模糊测试的函数为 FreeImage_test()
-nargs 2 -- E:\test.exe @@
python winafl-plot.py sync_dir out_dir
# sync_dir 是 afl-fuzz -o 参数所指定的路径,路径下应该包括 fuzzer_stats 文件;out_dir 目录表示结果输出目录,输出的是 index.html 文件,直接拉到浏览器中查看即可
using namespace std;
extern "C" __declspec(dllexport) int main(int argc, char** argv);
void FreeImage_test(HINSTANCE hinstLib, wchar_t* pathfile);
wchar_t* charToWChar(const char* text);
// 加载卸载函数
typedef DWORD(__stdcall *FreeImage_GetFileTypeU)(const wchar_t* lpszPathName, int flag);
typedef DWORD(__stdcall *FreeImage_Initialise)(BOOL load_local_plugins_only);
typedef DWORD(__stdcall *FreeImage_DeInitialise)();
typedef DWORD(__stdcall *FreeImage_LoadU)(DWORD format, const wchar_t* lpszPathName, int flag);
typedef DWORD(__stdcall *FreeImage_UnLoad)(DWORD dib);
// 图片格式判断函数
typedef DWORD(__stdcall *FreeImage_HasBackgroundColor)(DWORD dib); FreeImage_HasBackgroundColor HasBackgroundColor;
typedef DWORD(__stdcall *FreeImage_HasPixels)(DWORD dib); FreeImage_HasPixels HasPixels;
typedef DWORD(__stdcall *FreeImage_HasRGBMasks)(DWORD dib); FreeImage_HasRGBMasks HasRGBMasks;
typedef DWORD(__stdcall *FreeImage_IsTransparent)(DWORD dib); FreeImage_IsTransparent IsTransparent;
// 转换函数
typedef DWORD(__stdcall *FreeImage_ConvertTo24Bits)(DWORD dib); FreeImage_ConvertTo24Bits ConvertTo24Bits;
typedef DWORD(__stdcall *FreeImage_ConvertTo32Bits)(DWORD dib); FreeImage_ConvertTo32Bits ConvertTo32Bits;
FreeImage_Initialise Initialise;
FreeImage_GetFileTypeU LoadFileType;
FreeImage_LoadU LoadU; DWORD load;
FreeImage_UnLoad UnLoad;
FreeImage_DeInitialise DeInitialise;
int main(int argc, char** argv)
{
if (argc < 2) {
printf("Usage: %s <xml file>\n", argv[0]);
return 0;
}
wchar_t* PathName = charToWChar(argv[1]);
HINSTANCE hinstLib; BOOL fFreeResult, fRunTimeLinkSuccess = FALSE; DWORD Error = NULL;
hinstLib = LoadLibrary(TEXT("E:\\FreeImage.dll"));
if (hinstLib != NULL)
{
fRunTimeLinkSuccess = TRUE;
Initialise = (FreeImage_Initialise)GetProcAddress(hinstLib, (LPCSTR)163); // 初始化 FreeImage 库
LoadFileType = (FreeImage_GetFileTypeU)GetProcAddress(hinstLib, (LPCSTR)126);// 获取位图文件类型
LoadU = (FreeImage_LoadU)GetProcAddress(hinstLib, (LPCSTR)181); // 加载位图
UnLoad = (FreeImage_UnLoad)GetProcAddress(hinstLib, (LPCSTR)242);// 卸载位图
DeInitialise = (FreeImage_DeInitialise)GetProcAddress(hinstLib, (LPCSTR)83);//卸载 FreeImage 库
//图片格式加载函数
HasBackgroundColor = (FreeImage_HasBackgroundColor)GetProcAddress(hinstLib, (LPCSTR)160);
HasPixels = (FreeImage_HasPixels)GetProcAddress(hinstLib, (LPCSTR)161);
HasRGBMasks = (FreeImage_HasRGBMasks)GetProcAddress(hinstLib, (LPCSTR)162);
IsTransparent = (FreeImage_IsTransparent)GetProcAddress(hinstLib, (LPCSTR)168);
//转换函数
ConvertTo24Bits = (FreeImage_ConvertTo24Bits)GetProcAddress(hinstLib, (LPCSTR)65);
ConvertTo32Bits = (FreeImage_ConvertTo32Bits)GetProcAddress(hinstLib, (LPCSTR)66);
(Initialise)(FALSE);
FreeImage_test(hinstLib, PathName);
(DeInitialise)();
fFreeResult = FreeLibrary(hinstLib);
}
if (!fRunTimeLinkSuccess)
cout << "加载函数失败, Error: " << Error << endl;
return 0;
}
void FreeImage_test(HINSTANCE hinstLib, wchar_t* pathfile)
{
// 加载函数
DWORD FileType = (LoadFileType)(pathfile, 0);
load = (LoadU)(FileType, pathfile, 0);
//图片格式加载函数
HasBackgroundColor(load);
HasPixels(load);
HasRGBMasks(load);
IsTransparent(load);
//转换函数
ConvertTo24Bits(load);
ConvertTo32Bits(load);
(UnLoad)(load);
}
wchar_t* charToWChar(const char* text)
{
size_t size = strlen(text) + 1;
wchar_t* wa = new wchar_t[size];
mbstowcs(wa, text, size);
return wa;
}
看雪ID:护花使者cxy_
https://bbs.pediy.com/user-856782.htm
推荐文章++++
* Ubuntu 18.04.3 LTS 编译安卓6.0.0_r1